/*
 * GameStartingSplashWindow.java
 *
 * Created on May 4, 2011, 1:24:43 AM
 */

package client.gui;

import client.Main;
import client.util.GUIRoutines;
import client.util.UserProfile;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author danon
 */
public class GameStartingSplashWindow extends javax.swing.JDialog {

    /** Creates new form GameStartingSplashWindow */
    public GameStartingSplashWindow(MainWindow m, UserProfile rvl, boolean frst) {
        super(m, true);
        initComponents();
        jLabel1.setVisible(true);
        mainWindow = m;
        rival = rvl;
        first = frst;
        GUIRoutines.toScreenCenter(this);
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                boolean error = false;
                try{
                    initGame(rival, first);
                } catch (Exception ex){ error = true; }
                catch (Error er) { error = true; }
                dispose();
                if(error)
                    JOptionPane.showMessageDialog(mainWindow, java.util.ResourceBundle.getBundle("client/gui/Bundle").getString("cannot_init_game"), 
                            java.util.ResourceBundle.getBundle("client/gui/Bundle").getString("ERROR"), JOptionPane.ERROR_MESSAGE);
            }
        });
        timer.start();
    }
    
    
    public void initGame(UserProfile rival, boolean first){
        try{
            if(!Main.checkMemory(12*1024*1024))
                throw new OutOfMemoryError(java.util.ResourceBundle.getBundle("client/gui/Bundle").getString("low_memory"));
            if(rival==null){
                rival=new UserProfile(0);
                rival.update();
            }
            GameWindow g = new GameWindow(rival, first);
            g.setVisible(true);
            mainWindow.gameWindows.add(g);
        } catch (Error ex){
            System.err.println("Start game error: "+ex.getLocalizedMessage());
            JOptionPane.showMessageDialog(rootPane,
                    java.util.ResourceBundle.getBundle("client/gui/Bundle").getString("info")+ ex.getLocalizedMessage() + 
                    java.util.ResourceBundle.getBundle("client/gui/Bundle").getString("n_app_may_behave_abnormally"), "Titanic GameCilent: Error",
                    JOptionPane.ERROR_MESSAGE);
            System.gc();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 153, 51));
        setBounds(new java.awt.Rectangle(400, 300, 0, 0));
        setResizable(false);
        setUndecorated(true);

        jPanel1.setBackground(new java.awt.Color(0, 153, 51));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("client/gui/Bundle"); // NOI18N
        jLabel1.setText(bundle.getString("game_will_start_soon")); // NOI18N
        jLabel1.setDoubleBuffered(true);
        jLabel1.setFocusable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private Timer timer;
    private MainWindow mainWindow;
    private UserProfile rival;
    private boolean first;
}
